/*============================================================================*/
/*       UNIVERSIDAD DE GUADALAJARA - CONTINENTAL AUTOMOTIVE SYSTEMS          */
/*============================================================================*/
/*                        OBJECT SPECIFICATION                                */
/*============================================================================*
* %name:            %
* %version:         %
* %created_by:      %
* %date_created:    %
*=============================================================================*/
/* DESCRIPTION : Interrupts handling                                          */
/*============================================================================*/
/* FUNCTION COMMENT : Provices APIs to configure Interruptions                */   
/*                                                                            */
/*                                                                            */
/*============================================================================*/

#include "stdtypedef.h"
#include "p18f4550.h"
#include "interrupts.h"
#include "hw_timers.h"


/* APIs */
/**************************************************************
 *  Name                 : mw_interrupt_initCfg
 *  Description          : Initialize configuration for interruptions
 *  Parameters           :  none
 *  Return               :  none
 *  Critical/explanation :  No
 **************************************************************/
void mw_interrupt_initCfg()
{
	
	RCONbits.IPEN = 1;
/*  IPEN: Interrupt Priority Enable bit
    1 = Enable priority levels on interrupts
    0 = Disable priority levels on interrupts (PIC16CXXX Compatibility mode)
*/

    INTCON3bits.INT1IF = 0;
/*  INT1IF: INT1 External Interrupt Flag bit
    1 = The INT1 external interrupt occurred (must be cleared in software)
    0 = The INT1 external interrupt did not occur
*/

    INTCON3bits.INT2IF = 0;
/*  INT2IF: INT2 External Interrupt Flag bit
    1 = The INT2 external interrupt occurred (must be cleared in software)
    0 = The INT2 external interrupt did not occur
*/

    INTCON3bits.INT1IE = 0;
/*  INT1IE: INT1 External Interrupt Enable bit
    1 = Enables the INT1 external interrupt
    0 = Disables the INT1 external interrupt
*/

    INTCON3bits.INT2IE = 0;
/*  INT2IE: INT2 External Interrupt Enable bit
    1 = Enables the INT2 external interrupt
    0 = Disables the INT2 external interrupt
*/

    INTCON3bits.INT1IP = 0;
/*  INT1IP: INT1 External Interrupt Priority bit
    1 = High priority
    0 = Low priority
*/
    INTCON3bits.INT2IP = 0;
/*  INT2IP: INT2 External Interrupt Priority bit
    1 = High priority
    0 = Low priority
*/

    INTCON2bits.RBIP = 0;
/*  RBIP: RB Port Change Interrupt Priority bit
    1 = High priority
    0 = Low priority
*/

    INTCON2bits.TMR0IP = 1;
/*  TMR0IP: TMR0 Overflow Interrupt Priority bit
    1 = High priority
    0 = Low priority
*/

    INTCON2bits.INTEDG2 = 0;
/*  INTEDG2: External Interrupt 2 Edge Select bit
    1 = Interrupt on rising edge
    0 = Interrupt on falling edge
*/

    INTCON2bits.INTEDG1 = 0;
/*  INTEDG1: External Interrupt 1 Edge Select bit
    1 = Interrupt on rising edge
    0 = Interrupt on falling edge
*/

    INTCON2bits.INTEDG0 = 0;
/*  INTEDG0: External Interrupt 0 Edge Select bit
    1 = Interrupt on rising edge
    0 = Interrupt on falling edge
*/

    INTCON2bits.NOT_RBPU = 0;
/*  RBPU: PORTB Pull-up Enable bit
    1 = All PORTB pull-ups are disabled
    0 = PORTB pull-ups are enabled by individual port latch values
*/

    INTCONbits.RBIF = 0;
/*  RBIF: RB Port Change Interrupt Flag bit
    1 = At least one of the RB7:RB4 pins changed state (must be cleared in software)
    0 = None of the RB7:RB4 pins have changed state
*/

    INTCONbits.INT0IF = 0;
/*  INT0IF: INT0 External Interrupt Flag bit
    1 = The INT0 external interrupt occurred (must be cleared in software)
    0 = The INT0 external interrupt did not occur
*/

    INTCONbits.TMR0IF = 0;
/*  TMR0IF: TMR0 Overflow Interrupt Flag bit
    1 = TMR0 register has overflowed (must be cleared in software)
    0 = TMR0 register did not overflow
*/

    INTCONbits.RBIE = 0;
/*  RBIE: RB Port Change Interrupt Enable bit
    1 = Enables the RB port change interrupt
    0 = Disables the RB port change interrupt
*/

    INTCONbits.INT0IE = 0;
/*  INT0IE: INT0 External Interrupt Enable bit
    1 = Enables the INT0 external interrupt
    0 = Disables the INT0 external interrupt
*/

    INTCONbits.TMR0IE = 1;
/*  TMR0IE: TMR0 Overflow Interrupt Enable bit
    1 = Enables the TMR0 overflow interrupt
    0 = Disables the TMR0 overflow interrupt
*/

    INTCONbits.PEIE = 0;
/*  PEIE/GIEL: Peripheral Interrupt Enable bit
    When IPEN = 0:
    1 = Enables all unmasked peripheral interrupts
    0 = Disables all peripheral interrupts
    When IPEN = 1:
    1 = Enables all low priority peripheral interrupts
    0 = Disables all low priority peripheral interrupts
*/
    mw_interrupt_Enable_Interrupts();
}

/**************************************************************
 *  Name                 : mw_interrupt_Enable_Interrupts
 *  Description          : Enable interrupts
 *  Parameters           :  none
 *  Return               :  none
 *  Critical/explanation :  No
 **************************************************************/

void mw_interrupt_Enable_Interrupts()
{
    INTCONbits.GIE = 1;
/*	GIE/GIEH: Global Interrupt Enable bit
    When IPEN = 0:
    1 = Enables all unmasked interrupts
    0 = Disables all interrupts
    When IPEN = 1:
    1 = Enables all high priority interrupts
    0 = Disables all high priority interrupts
*/
}

/**************************************************************
 *  Name                 : mw_interrupt_Disable_Interrupts
 *  Description          : Disable interrupts
 *  Parameters           :  none
 *  Return               :  none
 *  Critical/explanation :  No
 **************************************************************/

void mw_interrupt_Disable_Interrupts()
{
    INTCONbits.GIE = 0;
/*	GIE/GIEH: Global Interrupt Enable bit
    When IPEN = 0:
    1 = Enables all unmasked interrupts
    0 = Disables all interrupts
    When IPEN = 1:
    1 = Enables all high priority interrupts
    0 = Disables all high priority interrupts
*/
}

